/**
 ********************************************************************
 * @author roocket
 * @file    sound.h
 * @version V0.0.0
 * @date    2024/4/26
 * @brief   This file is to indicate sound functions. 
 * @attention Code file rules:
 * rule: file encoding use UTF8;
 * rule: max line length 120 characters;
 * rule: line separator \r\n;
 * rule: use clion auto code format tool.
 */

#ifndef _SOUND_H_
#define _SOUND_H_
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <alsa/asoundlib.h>

struct sound_alsa_player
{
    /* handler for pcm */
    snd_pcm_t *pcm_hdl;
    
    /* hardware parameters */
    snd_pcm_hw_params_t *pcm_hw_params;

    /* hardware parameters */
    snd_pcm_hw_params_t *pcm_sw_params;

    /* rate of audio sample */
    unsigned int snd_rate;

    /* format of audio */
    snd_pcm_format_t snd_audio_format;

    /* method of streaming */
    unsigned int snd_pcm_stream_method;

    /* channel of audio */
    unsigned int snd_audio_channel;

    /* buffer or stream */
    char *audio_buffer;

    /* the count of each frame */
    int buffer_frame_cnt;

    /* frame byte */
    unsigned char frame_byte;

    snd_pcm_uframes_t psize_frames;

    snd_pcm_uframes_t bsize_frames;

};

struct mixer_info
{
    long vol_min;
    long vol_max;
    long left_val;
    long right_val;
};


/* init sound player */
int sound_alsa_player_init(const char *audio_device, struct sound_alsa_player *player);

/* deinit sound player */
void sound_alsa_player_deinit(struct sound_alsa_player *player);

/* capture audio data with callback */
int sound_alsa_capture_audio_data(struct sound_alsa_player *capture_player, int (*audio_cap_callback)(const char *buffer, int len));

/* playback audio data */
int sound_alsa_playback_audio_data(struct sound_alsa_player *playback_player, const char *buffer);

/* get volume from playback and capture */
int sound_alsa_get_playback_volume(const char *audio_device, const char *which_element, struct mixer_info *mixer_info);

int sound_alsa_set_playback_volume(const char *audio_device, const char *which_element, struct mixer_info *mixer_info);
#endif /* _SOUND_H_ */
